package com.ruoyi.web.controller.attend;

import com.ruoyi.attend.bean.DaySignExt;
import com.ruoyi.attend.domain.DaySign;
import com.ruoyi.attend.service.IDaySignService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.web.core.wang.utils.UUIDUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 签到 信息操作处理
 *
 * @author wangxx
 * @date 2018-11-26
 */
@Controller
@RequestMapping("/attend/daySign")
public class DaySignController extends BaseController
{
    private String prefix = "attend/daySign";

    @Autowired
    private IDaySignService daySignService;

    @RequiresPermissions("attend:daySign:view")
    @GetMapping()
    public String daySign()
    {
        return prefix + "/daySign";
    }

    /**
     * 查询签到列表
     */
    @RequiresPermissions("attend:daySign:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DaySign daySign)
    {
        startPage();
       //params.put("cardUserName","we");
        List<DaySign> list = daySignService.selectDaySignList(daySign);
        return getDataTable(list);
    }
    @RequiresPermissions("attend:daySign:list")
    @PostMapping("/listAll")
    @ResponseBody
    public TableDataInfo listAll(Map<String,Object> params)
    {
        startPage();
       //params.put("cardUserName","we");
        List<DaySign> list = daySignService.selectDaySignList(params);
        return getDataTable(list);
    }


    /**
     * 导出签到列表
     */
    @RequiresPermissions("attend:daySign:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DaySign daySign)
    {
        List<DaySign> list = daySignService.selectDaySignList(daySign);
        ExcelUtil<DaySign> util = new ExcelUtil<DaySign>(DaySign.class);
        return util.exportExcel(list, "daySign");
    }

    /**
     * 新增签到
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存签到
     */
    @RequiresPermissions("attend:daySign:add")
    @Log(title = "签到", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DaySign daySign)
    {
        return toAjax(daySignService.insertDaySign(daySign));
    }

    /**
     * 修改签到
     */
    @GetMapping("/edit/{signId}")
    public String edit(@PathVariable("signId") String signId, ModelMap mmap)
    {
        DaySign daySign = daySignService.selectDaySignById(signId);
        mmap.put("daySign", daySign);
        return prefix + "/edit";
    }

    /**
     * 修改保存签到
     */
    @RequiresPermissions("attend:daySign:edit")
    @Log(title = "签到", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DaySign daySign)
    {
        return toAjax(daySignService.updateDaySign(daySign));
    }

    /**
     * 删除签到
     */
    @RequiresPermissions("attend:daySign:remove")
    @Log(title = "签到", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(daySignService.deleteDaySignByIds(ids));
    }
    /**
     * 记录签到次数
     **/
    @RequiresPermissions("attend:daySign:sign")
    @PostMapping("/signPlus")
    @Log(title = "打卡记录", businessType = BusinessType.OTHER)
    @ResponseBody
    public AjaxResult sign(String ids) {
        String[] signIds = Convert.toStrArray(ids);
        DaySign daySign =null;
        for (String signId : signIds) {
            daySign = daySignService.selectDaySignById(signId);
            if (daySign.getUserId()!=null){
                daySign.setUserId(daySign.getUserId());
                Integer num = Integer.valueOf(daySign.getSignCount())+1; //变化次数
                daySign.setSignCount(num.toString());
                daySign.setSignId(UUIDUtil.getUUID());//随机num号码
                daySignService.insertDaySign(daySign);
            }
        }
        return toAjax(1);
    }
}
